#! /usr/bin/env bash 

command=$1

if [ -z "$command" ]
then
	command=start
fi

if [[ ( ! "halt" = "$command" ) && ( ! "list" = "$command" ) && ( ! "restart" = "$command") && ( ! "start" = "$command" ) && ( ! "stop" = "$command" ) ]]
then
	echo Usage: $(basename "$0") "<start|list|restart|stop|halt>"
	exit
fi

if [ ! -w "$PWD" ]
then
	echo $(date +"%Y-%m-%d %H:%M:%S") Mounting filesystem for write access
	mount -o remount rw /
fi

cd "$(dirname "$0")"

rfkill list | grep "blocked: yes" > /dev/null
if [ 0 -eq $? ]
then
	echo $(date +"%Y-%m-%d %H:%M:%S") Unblocking radio
	rfkill unblock 0
	rfkill unblock 1
	rfkill unblock 2
	rfkill unblock 3

	rfkill list | grep "blocked: yes" > /dev/null
	if [ 0 -eq $? ]
	then
		echo ERROR: Radio is blocked
		exit
	fi
fi

mkdir -p capture
mkdir -p database
mkdir -p scan

./command/$command.sh

